package com.bra.ui.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

import com.bra.R;
import com.bra.request.UrlConstants;
import com.bra.ui.base.BaseActivity;
import com.bra.utils.SPHelper;
import com.bra.utils.StringHelper;
import com.bra.utils.ToastUtil;
import com.google.gson.JsonObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.view.UMFriendListener;

/**
 * 登录界面
 * 
 * @author lenovo
 *
 */
public class LoginActivity extends BaseActivity {
	private TextView login_regist,login_forget_pw;
	private ImageView activity_back,login_click_weiixn,login_click_qq;
	private TextView activity_title;
	//首先获取UMShareAPI
	private UMShareAPI mShareAPI;
	private EditText login_phone,login_pw;
	private Button login_btu;
	private String PN,PW;
	private String LoginUrl = UrlConstants.IP;
	private SHARE_MEDIA platform = null;

	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_login, null);
	}

	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
		mShareAPI = UMShareAPI.get(LoginActivity.this);
		activity_title = (TextView) findViewById(R.id.activity_title);
		activity_back = (ImageView) findViewById(R.id.activity_back);
		activity_title.setText("登录");
		login_regist = (TextView) findViewById(R.id.login_regist);
		login_regist.setVisibility(View.VISIBLE);
		login_click_weiixn = (ImageView) findViewById(R.id.login_click_weiixn);
		login_click_qq = (ImageView) findViewById(R.id.login_click_qq);
		
		login_forget_pw= (TextView) findViewById(R.id.login_forget_pw);
		login_phone= (EditText) findViewById(R.id.login_phone);
		login_pw= (EditText) findViewById(R.id.login_pw);
		login_btu= (Button) findViewById(R.id.login_btu);
	}
	private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText( getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };

	@Override
	protected void setListener() {
		activity_back.setOnClickListener(click);
		login_regist.setOnClickListener(click);
		login_click_weiixn.setOnClickListener(click);
		login_click_qq.setOnClickListener(click);		
		login_forget_pw.setOnClickListener(click);
		login_btu.setOnClickListener(click);
	}

	private View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
						
			Bundle bundle = new Bundle();
			switch (v.getId()) {
			case R.id.activity_back:
				finish();
				break;
			case R.id.login_btu:
				PN=login_phone.getText().toString().trim();
				PW=login_pw.getText().toString().trim();
				if(StringHelper.isEmpty(PN)){				
					ToastUtil.showToast(LoginActivity.this, "请输入手机号");
					return;
				} 
				if(!StringHelper.isMobileNO(PN)){				
					ToastUtil.showToast(LoginActivity.this, "手机号不合法");
					return;
				} 
				if(StringHelper.isEmpty(PW)){				
					ToastUtil.showToast(LoginActivity.this, "请输入密码");
					return;
				} 
				
				if(PW.length()<6){				
					ToastUtil.showToast(LoginActivity.this, "密码(6-16位)");
					return;
				} 	
				httpRequestLoginInfo(LoginActivity.this,LoginUrl,PN,PW);
				break;				
			case R.id.login_regist:								
				goActivity(RegistActivity.class, bundle);								
				break;
			case R.id.login_forget_pw:							
				goActivity(RegistActivity.class, bundle);
				
				break;
			case R.id.login_click_weiixn:				
				platform = SHARE_MEDIA.WEIXIN;
				 mShareAPI.doOauthVerify(LoginActivity.this, platform, umAuthListener);
				break;
			case R.id.login_click_qq:
				 platform = SHARE_MEDIA.QQ;
				 mShareAPI.doOauthVerify(LoginActivity.this, platform, umAuthListener);
				break;			
			default:
				break;
			}

		}
	};	
	protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {
		  mShareAPI.onActivityResult(requestCode, resultCode, data);
		
	};
	
	private void httpRequestLoginInfo(Context context, String url,String number,String password) {		
		
		 AsyncHttpClient client = new AsyncHttpClient();  	     
       // 创建请求参数  
	        RequestParams params = new RequestParams();  	        
	        params.put("userId", "");
	        params.put("equipment", "android");
	        params.put("requestType", "Login_Api");
	        params.put("token", ""); 
	        params.put("mobile", number);
	        params.put("password", password);
	        
	        // 执行post方法  
	        client.post(url, params, new JsonHttpResponseHandler(){

				@Override
				public void onFailure(int statusCode, Header[] headers,
						Throwable throwable, JSONObject errorResponse) {
					// TODO Auto-generated method stub
					super.onFailure(statusCode, headers, throwable, errorResponse);
					Toast.makeText(LoginActivity.this, "网络错误", 0).show();
				}

				@Override
				public void onSuccess(int statusCode, Header[] headers,
						JSONObject response) {
					// TODO Auto-generated method stub
					super.onSuccess(statusCode, headers, response);
					System.out.println(response.toString()+"====");				
					try {
						JSONObject object=new JSONObject(response.toString());
						String userid=object.getString("userId");
						String token=object.getString("token");
						SPHelper.saveString(getApplicationContext(), "userId", userid);
						SPHelper.saveString(getApplicationContext(), "token", token);						
						SPHelper.saveBoolean(getApplicationContext(), "Is_Login", true);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}											
				}
	        	
	        });  
	}
}
